package com.itheima.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.goods.pojo.Spec;
import com.itheima.goods.service.SpecService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author mySun
 * @create 2021-07-10 21:44
 */
@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecService specService;

    @GetMapping
    public Result findAll(){
        List<Spec> specs = specService.list();
        return new Result<>(true,StatusCode.OK,"",specs);
    }

    @PostMapping
    public Result add(@RequestBody  Spec spec){
        specService.save(spec);
        return new Result(true,StatusCode.OK,"");
    }

    @GetMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable("page")Integer curPage,@PathVariable("size")Integer size){
        Page<Spec> page = new Page<>(curPage,size);
        specService.page(page);

        PageResult<Spec> pageResult= new PageResult<>();
        pageResult.setRows(page.getRecords());
        pageResult.setTotal(page.getTotal());
        return new Result<>(true,StatusCode.OK,"",pageResult);
    }

    @PostMapping("/search/{page}/{size}")
    public Result findPageByMap(
            @PathVariable("page") Integer curPage,
            @PathVariable("size") Integer size,
            @RequestBody(required = false) Map<String,Object> searchMap){
        Page<Spec> page = new Page<>(curPage,size);
        LambdaQueryWrapper<Spec> lambdaQuery = Wrappers.lambdaQuery(Spec.class);
        if(searchMap != null){
            lambdaQuery.like(ObjectUtils.isNotEmpty(searchMap.get("name")),Spec::getName,searchMap.get("name"))
                    .like(ObjectUtils.isNotEmpty(searchMap.get("options")),Spec::getOptions,searchMap.get("options"))
                    .eq(ObjectUtils.isNotEmpty(searchMap.get("seq")),Spec::getSeq,searchMap.get("seq"));
        }

        specService.page(page,lambdaQuery);

        PageResult<Spec> pageResult = new PageResult<>();
        pageResult.setRows(page.getRecords());
        pageResult.setTotal(page.getTotal());

        return new Result<>(true,StatusCode.OK,"",pageResult);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id")Integer id){
        Spec spec = specService.getById("id");
        return new Result<>(true,StatusCode.OK,"",spec);
    }

    @PutMapping("{id}")
    public Result updateById(@RequestBody Spec spec){
        specService.updateById(spec);
        return new Result(true,StatusCode.OK,"");
    }

    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable Integer id){
        specService.removeById(id);
        return new Result(true,StatusCode.OK,"");
    }





}
